//
//  receiveTelepBeLight.m
//  tcpoctest
//
//  Created by apple on 13-8-1.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "receiveTelepBeLight.h"
#import "sendMessageReceipt.h"
#import "telepathyInfo.h"
#import "AudioPlayManager.h"
#import "XMMessageInfo.h"
#import "ChatContentInfo.h"
#import "MessageDataSource.h"
#import "chatDataSource.h"
#import "chatuserdb.h"
#import "messagedb.h"
#import "TelepathyDataSource.h"
#import "telepathydb.h"

@implementation receiveTelepBeLight

-(void)processreceive
{
    return;
    //接收  16char(1),225char(1),发送者的用户名长度char(1),
    //发送者的用户名char(x),[用户名长度char(1),
    //用户名char(x)]*10,第10个是正确用户,消息ID长度char(1),
    //消息ID长度char(x),时间长度char(1),时间char(x)
    NSString *sendmail = [logicsb ReadUnencodeString];
    
    [logicsb ReadIntWithTT:NTT_BIG2SMALL];
    
    telepathyInfo *tinfo = [[telepathyInfo alloc] init];
    
    
    
    for (int i = 0; i < 9; ++i) {
        
        NSString *strmail = [logicsb ReadUnencodeString];
        
        [tinfo.mailarr addObject:strmail];
        [tinfo.guessDic setObject:[NSNumber numberWithInt:TGT_GUESS] forKey:strmail];
        
    }
    
    tinfo.correctmail = [logicsb ReadUnencodeString];
    
    NSString *strid = [logicsb ReadUnencodeString];
    NSString *strtime = [logicsb ReadUnencodeString];
    
    tinfo.strid = strid;
    tinfo.iGuessNum = 0;
    tinfo.iStateType = TGT_GUESS;
    
    [[DataControl shareControl].TelepathyDS AddTelepDic:tinfo];
    [[DataBaseCtrol shareControl].TelepDB insertTelepInfo:tinfo];
    
    XMMessageInfo *minfo = [[XMMessageInfo alloc] init];
    minfo.strmail = sendmail;
    minfo.messageType = XMMT_CHAT;
    minfo.mbAlreadayArgee = false;
    minfo.mindex = [[DataControl shareControl].messageDS  GetMagArrCount];
    minfo.strmsgid = [[DataControl shareControl].messageDS getMsgID];
    
    XMMessageInfo *mexits = [[MessageManager sharedManager] getMessage:minfo];
    ChatContentInfo *cinfo = [[ChatContentInfo alloc] init];
    
    if (mexits == nil)
    {
        
        NSMutableArray *chatshowarr = [[NSMutableArray alloc] init];
        [[DataControl shareControl].chatDS SetChatDicArr:chatshowarr withKey:minfo.strmail];
        [[DataBaseCtrol shareControl].chatuserdatabase insertChatDic:minfo.strmail withIsGroup:false];
        
        
        cinfo.mail = sendmail;
        cinfo.chatType = CT_TELEPATHY;
        cinfo.strChatDate = strtime;
        cinfo.iSequence = [LogicControl sharedLogicControl].CurSequence;
        cinfo.isSelf = false;
        cinfo.strid = strid;
        cinfo.strtext = @"有人对你一见钟情,快去心电感应一下!";
        
        [chatshowarr addObject:cinfo];
        
        
        minfo.badgeNum++;
        minfo.strtime = cinfo.strChatDate;
        [[MessageManager sharedManager] AddMessage:minfo];
        
        [[DataControl shareControl].chatDS addChatInfo:cinfo withID:strid];
        
        [[DataBaseCtrol shareControl].chatuserdatabase insertChatInfo:cinfo];
        [[DataControl shareControl].UICommendDS addUICommand:UC_RECIVECHECK];
    }
    else
    {
        
        NSMutableArray *chatshowarr = [[DataControl shareControl].chatDS GetArrByKey:sendmail];
        if (chatshowarr != nil)
        {
            cinfo.mail = sendmail;
            cinfo.chatType = CT_TELEPATHY;
            cinfo.strChatDate = strtime;
            cinfo.iSequence = [LogicControl sharedLogicControl].CurSequence;
            cinfo.isSelf = false;
            cinfo.strid = strid;
            cinfo.strtext = @"有人对你一见钟情,快去心电感应一下!";
            
            [chatshowarr addObject:cinfo];
            
            [[DataControl shareControl].chatDS addChatInfo:cinfo withID:strid];
            
            [[DataBaseCtrol shareControl].chatuserdatabase insertChatInfo:cinfo];
        }
        
        mexits.strtime = cinfo.strChatDate;
        
        if ([DataControl shareControl].iGameState == GMS_CHAT &&
            [[DataControl shareControl].currentFI.mail isEqualToString:sendmail])
        {
            [[DataControl shareControl].UICommendDS addUICommand:UC_UPDATACHAT];
        }
        else
        {
            if (mexits.mbAlreadayArgee == true)
            {
                mexits.mbAlreadayArgee = false;
            }
            
            mexits.badgeNum++;
            
            [[DataControl shareControl].UICommendDS addUICommand:UC_RECIVECHECK];
        }
        
        [[DataBaseCtrol shareControl].messagedatabase updateMessage:mexits];
        
        minfo = nil;
    }
    
    [[netManager sharedNetManager].sendMsgReceipt SendMegReceipt:strid];
    
    [self ShowLog];
    
    EACHOTHER_LOG(@"receive length %d mainindex %d secindex %d %@ \r\n"
                  ,logicsb.mrestLength
                  ,imainIndex
                  ,isecIndex
                  ,self);
    
    [[AudioPlayManager shareControl] playSystem:1002];
}

@end
